﻿using System;
using System.Collections.Generic;
using System.Data;
using Assist;
using HYElectricCommon;
using HYFamilyLibDataManager;

namespace HYRElectric.Calculate
{
	internal class FindUseFactor
	{
		public FindUseFactor(DGVRowData data)
		{
			string fileName = HYElectricCommon.AssistFunc.GetAppRootPath() + "\\Data\\IlluminanceCal.db3";
			this.dbIllAndPower = new SqliteDatabase(fileName);
			this.m_dgvRowData = data;
		}

		public double GetUseFactor()
		{
			double num = 0.0;
			if (this.InitUseFactorList() && this.InitCurUseFactor())
			{
				num = new StuRoot().Cal();
			}
			return FindUseFactor.m_stuUseFactor.dUseFactor = ((num < 0.0) ? 0.0 : num);
		}

		private bool InitUseFactorList()
		{
			string sDJXH = this.m_dgvRowData.djcsData.sDJXH;
			if (sDJXH == null || sDJXH == "")
			{
				return false;
			}
			DataTable dataTable = this.dbIllAndPower.ExecQuery("select [ID] from [Lamp] where [LampStyle] ='" + sDJXH + "'");
			if (dataTable == null)
			{
				return false;
			}
			string str = dataTable.Rows[0][0].ToString();
			DataTable dataTable2 = this.dbIllAndPower.ExecQuery("select [CeiRef], [WallRef], [GroundRef], [RCR_RI], [UtilizationFactor] from [LYXS] where [LuminaireID] ='" + str + "'");
			if (dataTable2 == null)
			{
				return false;
			}
			FindUseFactor.m_arUseFactor.Clear();
			for (int i = 0; i < dataTable2.Rows.Count; i++)
			{
				StuUseFactor stuUseFactor = new StuUseFactor();
				stuUseFactor.dCeiling = Convert.ToDouble(dataTable2.Rows[i][0].ToString());
				stuUseFactor.dWall = Convert.ToDouble(dataTable2.Rows[i][1].ToString());
				stuUseFactor.dFloor = Convert.ToDouble(dataTable2.Rows[i][2].ToString());
				stuUseFactor.dRCR_RI = Convert.ToDouble(dataTable2.Rows[i][3].ToString());
				stuUseFactor.dUseFactor = Convert.ToDouble(dataTable2.Rows[i][4].ToString());
				FindUseFactor.m_arUseFactor.Add(stuUseFactor);
			}
			return FindUseFactor.m_arUseFactor.Count > 0;
		}

		private bool InitCurUseFactor()
		{
			UseFactorType useFactorType = UseFactorType.RCR;
			if (!this.FindUseFactorType(ref useFactorType))
			{
				return false;
			}
			CalRoomParam calRoomParam = new CalRoomParam(this.m_dgvRowData);
			if (useFactorType == UseFactorType.RCR)
			{
				FindUseFactor.m_stuUseFactor.dRCR_RI = calRoomParam.RoomRCR();
			}
			else
			{
				FindUseFactor.m_stuUseFactor.dRCR_RI = calRoomParam.RoomRI();
			}
			FindUseFactor.m_stuUseFactor.dCeiling = (double)calRoomParam.PCeiling();
			FindUseFactor.m_stuUseFactor.dWall = (double)calRoomParam.PWall();
			FindUseFactor.m_stuUseFactor.dFloor = (double)calRoomParam.PFloor();
			FindUseFactor.m_stuUseFactor.dUseFactor = 0.0;
			if (GeoUtil.IsEqual(FindUseFactor.m_stuUseFactor.dCeiling, 0.0, 0.1) || GeoUtil.IsEqual(FindUseFactor.m_stuUseFactor.dWall, 0.0, 0.1) || GeoUtil.IsEqual(FindUseFactor.m_stuUseFactor.dFloor, 0.0, 0.1))
			{
				FindUseFactor.m_stuUseFactor.dCeiling = 0.0;
				FindUseFactor.m_stuUseFactor.dWall = 0.0;
				FindUseFactor.m_stuUseFactor.dFloor = 0.0;
			}
			return true;
		}

		private bool FindUseFactorType(ref UseFactorType type)
		{
			string sDJXH = this.m_dgvRowData.djcsData.sDJXH;
			if (sDJXH == null || sDJXH == "")
			{
				return false;
			}
			DataTable dataTable = this.dbIllAndPower.ExecQuery("select [UtilizationFactorType] from [Lamp] where [LampStyle] ='" + sDJXH + "'");
			if (dataTable == null)
			{
				return false;
			}
			if (dataTable.Rows[0][0].ToString().Trim() == "0")
			{
				type = UseFactorType.RCR;
			}
			else
			{
				type = UseFactorType.RI;
			}
			return true;
		}

		public static List<StuUseFactor> m_arUseFactor = new List<StuUseFactor>();

		public static StuUseFactor m_stuUseFactor = new StuUseFactor();

		private DGVRowData m_dgvRowData;

		private SqliteDatabase dbIllAndPower;
	}
}
